﻿@page "/Install"
@using System.IO.Compression
@using JxAudio.Web.Components.Layout
@using JxAudio.Web.Components.Components.Install
@using JxAudio.Web.Vo
@using Microsoft.Extensions.Localization
@using JxAudio.Core.Options
@using JxAudio.Core
@using JxAudio.Web.Enums

@inject IStringLocalizer<Install> InstallLocalizer

@layout EmptyLayout

<div class="container">
    <Step @ref="_step" Items="_stepItems" class="install_step">
        <FinishedTemplate>
            <FinishStep DbConfigOption="_dbConfigOption" SettingsVo="_settingsVo" UserInstallVo="_userInstallVo"></FinishStep>
        </FinishedTemplate>
    </Step>
</div>

@code {
    private readonly List<StepOption> _stepItems = new List<StepOption>();

    private Step? _step;

    private DbConfigOption _dbConfigOption = new() { DbType = "Sqlite", Prefix = "jx_", DbName = $"{Application.WebHostEnvironment.ContentRootPath}/config/JxAudio.db" };

    private SettingsVo _settingsVo = new(){TimeUnit = TimeUnit.Day, SearchType = SearchType.Interval, ScanInterval = "1"};

    private UserInstallVo _userInstallVo = new() { UserName = "Admin"};

    protected override void OnInitialized()
    {
        base.OnInitialized();
        if (Directory.Exists("./plugins"))
        {
            foreach (var plugin in Directory.GetFiles("./plugins", "*.jxaudio"))
            {
                var pluginName = Path.GetFileNameWithoutExtension(plugin);
                if (!Directory.Exists($"./config/plugins/{pluginName}"))
                {
                    ZipFile.ExtractToDirectory(plugin, "./config/plugins/");
                }
            }
        }
        // _stepItems.Add(new StepOption()
        // {
        //     Template = BootstrapDynamicComponent.CreateComponent<CultureSelect>(new Dictionary<string, object?>()
        //     {
        //         {nameof(StepBase.Next), () => { _step?.Next();StateHasChanged(); }},
        //         {nameof(StepBase.Prev), () => { _step?.Prev();StateHasChanged(); }}
        //     }).Render(),
        //     Description = InstallLocalizer["LanguageDesc"],
        //     Title = InstallLocalizer["Language"],
        //     FinishedIcon = "fas fa-check"
        // });
        _stepItems.Add(new StepOption()
        {
            Template = BootstrapDynamicComponent.CreateComponent<DbSetup>(new Dictionary<string, object?>()
            {
                {nameof(StepBase.Next), () => { _step?.Next();StateHasChanged(); }},
                {nameof(StepBase.Prev), () => { _step?.Prev();StateHasChanged(); }},
                {nameof(DbSetup.DbConfig), _dbConfigOption}
            }).Render(),
            Description = InstallLocalizer["DbDesc"],
            Title = InstallLocalizer["Db"],
            FinishedIcon = "fas fa-check"
        });
        _stepItems.Add(new StepOption()
        {
            Template = BootstrapDynamicComponent.CreateComponent<SettingStep>(new Dictionary<string, object?>()
            {
                {nameof(StepBase.Next), () => { _step?.Next();StateHasChanged(); }},
                {nameof(StepBase.Prev), () => { _step?.Prev();StateHasChanged(); }},
                {nameof(SettingStep.SettingsVo), _settingsVo}
            }).Render(),
            Description = InstallLocalizer["SettingsDesc"],
            Title = InstallLocalizer["Settings"],
            FinishedIcon = "fas fa-check"
        });
        _stepItems.Add(new StepOption()
        {
            Template = BootstrapDynamicComponent.CreateComponent<UserSetup>(new Dictionary<string, object?>()
            {
                {nameof(StepBase.Next), () => { _step?.Next();StateHasChanged(); }},
                {nameof(StepBase.Prev), () => { _step?.Prev();StateHasChanged(); }},
                {nameof(UserSetup.UserInstallVo), _userInstallVo}
            }).Render(),
            Description = InstallLocalizer["UserDesc"],
            Title = InstallLocalizer["User"],
            FinishedIcon = "fas fa-check"
        });
    }

}